package com.example.agriculyural.Facade;

import com.example.agriculyural.Chain.EmergencyStopHandler;
import com.example.agriculyural.Chain.FaultDetectionHandler;
import com.example.agriculyural.Chain.ObstacleDetectionHandler;
import com.example.agriculyural.Chain.SafetyHandler;
import com.example.agriculyural.Command.Command;
import com.example.agriculyural.Command.RemoteController;
import com.example.agriculyural.Command.StartMachineCommand;
import com.example.agriculyural.FactoryMethod.AgriculturalMachine;
import com.example.agriculyural.Observer.EnvironmentData;
import com.example.agriculyural.Observer.EnvironmentMonitoringCenter;
import com.example.agriculyural.SingletonPattern.GPSNavigationSystem;
import com.example.agriculyural.State.MachineStateContext;
import com.example.agriculyural.State.MaintenanceState;
import com.example.agriculyural.State.WorkingState;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SmartFarmSystemFacade {
    private EnvironmentMonitoringCenter monitoringCenter;
    private GPSNavigationSystem gpsSystem;
    private RemoteController controller;
    private List<AgriculturalMachine> machines;
    private Map<AgriculturalMachine, MachineStateContext> stateContexts;

    public SmartFarmSystemFacade() {
        this.monitoringCenter = new EnvironmentMonitoringCenter();
        this.gpsSystem = GPSNavigationSystem.getInstance();
        this.controller = new RemoteController();
        this.machines = new ArrayList<>();
        this.stateContexts = new HashMap<>();

        System.out.println("智能农场系统初始化完成");
    }

    public void addMachine(AgriculturalMachine machine) {
        machines.add(machine);
        monitoringCenter.addObserver(machine);
        stateContexts.put(machine, new MachineStateContext(machine));
        System.out.println("添加农机设备: " + machine.getMachineType() + " " + machine.getId());
    }

    public void startAllMachines() {
        System.out.println("\n=== 启动所有农机设备 ===");
        for (AgriculturalMachine machine : machines) {
            MachineStateContext context = stateContexts.get(machine);
            context.setState(new WorkingState());
            context.executeState();

            Command startCommand = new StartMachineCommand(machine);
            controller.executeCommand(startCommand);
        }
    }

    public void updateEnvironmentData(EnvironmentData data) {
        System.out.println("\n=== 更新环境数据 ===");
        gpsSystem.updatePosition(39.9042 + Math.random() * 0.01, 116.4074 + Math.random() * 0.01);
        monitoringCenter.setEnvironmentData(data);
    }

    public void handleEmergency(String issue) {
        System.out.println("\n=== 处理紧急情况: " + issue + " ===");

        // 设置责任链
        SafetyHandler obstacleHandler = new ObstacleDetectionHandler();
        SafetyHandler faultHandler = new FaultDetectionHandler();
        SafetyHandler emergencyHandler = new EmergencyStopHandler();

        obstacleHandler.setNextHandler(faultHandler);
        faultHandler.setNextHandler(emergencyHandler);

        // 处理所有机器的安全问题
        for (AgriculturalMachine machine : machines) {
            obstacleHandler.handleSafety(issue, machine);

            if (issue.contains("紧急") || issue.contains("故障")) {
                MachineStateContext context = stateContexts.get(machine);
                context.setState(new MaintenanceState());
                context.executeState();
            }
        }
    }

    public void showSystemStatus() {
        System.out.println("\n=== 系统状态报告 ===");
        System.out.println(gpsSystem.getCurrentPosition());
        System.out.println("农机设备状态:");

        for (AgriculturalMachine machine : machines) {
            MachineStateContext context = stateContexts.get(machine);
            System.out.println("- " + machine.getMachineType() + " " + machine.getId() +
                    ": " + context.getCurrentStateName());
        }
    }
}

